package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.News;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.feign.NewsClient;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysNoticeService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
@RequiredArgsConstructor
public class SysNoticeServiceImpl implements ISysNoticeService {
    @Autowired
    private SysNoticeMapper noticeMapper;

    private final  NewsClient newsClient;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @GlobalTransactional
    @Override
    public int insertNotice(SysNotice notice) {
        int count = 0;
            count = noticeMapper.insertNotice(notice);
           News news = new News();
           news.setTitle(notice.getNoticeTitle());
           news.setNoticeId(notice.getNoticeId());
           news.setContent(notice.getNoticeContent());
           news.setStatus("1");
           news.setSource("通知公告");
           news.setTypeId(6);
        news.setStatus("1");
           newsClient.add(news);
        return count;
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @GlobalTransactional
    @Override
    public int updateNotice(SysNotice notice) {
        int count = noticeMapper.updateNotice(notice);
        if (count > 0) {
            //将修改的公告信息同步到新闻系统
            News news = new News();
            //查找新闻的ID
            news.setNoticeId(notice.getNoticeId());
            news.setTitle(notice.getNoticeTitle());
            news.setContent(notice.getNoticeContent());
            news.setSource("通知公告");
            if ("0".equals(notice.getStatus())) {
                news.setStatus("1");
            } else if ("1".equals(notice.getStatus())) {
                news.setStatus("0");
            }
            news.setTypeId(6);
            news.setUpdateBy(SecurityUtils.getUsername());
            news.setUpdateTime(new Date());
            //远程调用新闻系统修改新闻
            newsClient.updateByNoticeId(news);
        }
        return count;
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        int count = noticeMapper.deleteNoticeById(noticeId);
        if (count > 0){
            newsClient.remove(new Long[]{noticeId});
        }
        return count;
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @GlobalTransactional
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        int count = noticeMapper.deleteNoticeByIds(noticeIds);
        if (count > 0){
            newsClient.removeByNoticeIds(noticeIds);
        }
        return count;
    }
}
